#/usr/bin/python
import os
import hashlib
import time
import json
import stat
import mimetypes
import sys
import gzip

root = "/home/patrick/netbeans-7.2"
#root = "/home/patrick"
#root = "/run/media/patrick/3b07aa79-2b27-4c0f-b829-f4ad49a105cf/home/patrick/X-Files/03"
name = "Test"

class profile():
    def __init__(self):
        self.profile_name = ""
        self.ctime = ""
        self.mtime = ""
        self.c_revision = 0
        self.t_revision = 0
        self.comment = ""
        self.element = {}
        mimetypes.init()

    def name(self, name):
        self.profile_name = name

    def search(self, info, condition):
        print "To do" 

    def add(self, path):
        properties = {}
        revisions = {}
        stats = self.get_stat(path)
        if stats["type"] not in ["d", "s", "l"]:
            signature = self.create_signature(path)
            properties["signature"] = signature
            properties["types"] = self.guess_type(path)
        properties["stats"] = stats
        properties["comment"] = ""
        self.element[path] = revisions["r0"] = properties

    def remove(self, path):
        print "to do"

    def load(self, name):
        print "to do"

    def save(self):
        f = gzip.open('test.gzip', 'w')
        f.write(json.dumps(self.element, sort_keys=True, indent=4))
        f.flush()
        os.fsync(f.fileno())
        f.close()
    
    def guess_type(self, path):
        mtype = {}
        t, e = mimetypes.guess_type("file://" + path , strict=True)
        mtype["type"] = t
        mtype["encoding"] = e
        return mtype
        

    def create_signature(self, path):
        #Possible md5, sha1,
        signatures = {}
        sha1 = hashlib.sha1()
        md5 = hashlib.md5()
        f = open(path)
        while 1:
            buf = f.read(2048)
            if not buf:
                break
            else:
                sha1.update(buf)
                md5.update(buf)

        f.close()
        del f
        signatures["sha1"] = sha1.hexdigest()
        signatures["md5"] = md5.hexdigest()
        
        return signatures


    def get_stat(self, path):
        stats = os.lstat(path)
        table_stats = {}
        mode = stats[0]
        if stat.S_ISDIR(mode):
            table_stats["type"] = "d"
        elif stat.S_ISCHR(mode):
            table_stats["type"] = "c"
        elif stat.S_ISBLK(mode):
            table_stats["type"] = "b"
        elif stat.S_ISREG(mode):
            table_stats["type"] = "f"
        elif stat.S_ISFIFO(mode):
            table_stats["type"] = "p"
        elif stat.S_ISLNK(mode):
            table_stats["type"] = "l"
        elif stat.S_ISSOCK(mode):
            table_stats["type"] = "s"
        else:
            table_stats["type"] = "u"
        #for i in stats:
            #table_stats.append(i)
        table_stats["mode"] = stat.S_IMODE(mode)
        table_stats["inode"] = stats[1]
        table_stats["device"] = stats[2]
        table_stats["hardlink"] = stats[3]
        table_stats["uid"] = stats[4]
        table_stats["gid"] = stats[5]
        table_stats["size"] = stats[6]
        table_stats["atime"] = stats[7]
        table_stats["mtime"] = stats[8]
        table_stats["ctime"] = stats[9]
        return table_stats

pro = profile()   
pro.name("test")

print "Start scanning..."
all_files = []
all_directories = []
for (root, dirs, files) in os.walk(root):
    for f in files:
        p = os.path.join(root, f)
        all_files.append(p)
    for d in dirs:
        p = os.path.join(root, d)
        all_directories.append(p)
print "Number of directories : " + str(len(all_directories))
print "Number of files : " + str(len(all_files))
for d in all_directories:
    pro.add(d)
for f in all_files:
    pro.add(f)

pro.save()




